<?php
/*
 * $Id: ExampleProtocol.php,v 1.5 2009-04-19 13:42:59 root Exp $
 */
require_once(realpath(dirname(__FILE__)).'/SocketServer.php');

class MyProtocol extends tSocketProtocol{
	function onConnect($Connection){
		$this->WriteToAll($Connection->id."!connect\r\n");
	}
	function onDisconnect($Connection){
		echo $Connection->id."!disconnect\r\n";
		$this->WriteToAll($Connection->id."!disconnect\r\n");
	}
	function onData($Connection){
		// Найдем позицию разделителя
		$p = strpos($Connection->ReadBUF, "\n");
		if ($p === false) return;
		// Вырежем команду
		$cmd = substr($Connection->ReadBUF,0,$p);
		$Connection->ReadBUF = substr($Connection->ReadBUF, $p+1);
		if (trim($cmd) == 'quit'){
			//echo "quit detach\r\n";
			$this->ConnectionManager->detach($Connection);
		}else{
			$this->WriteToAll($Connection->id.":".$cmd."\r\n");
		}
	}
}
class TestHTTPProtocol extends tSocketProtocol{
	function onData($Connection){
		$Connection->Write("HTTP/1.1 200 OK\r\nContenth-type: text/html; charset=UTF-8\r\n\r\nOK");
		$Connection->Close();
	}
}

$server = new tSocketServer();
$server->attachProtocol(new MyProtocol());
// accepting connections on port 4111 and 4112
if ($server->Bind('127.0.0.1', 4111)){
	if ($server->Bind('127.0.0.1', 4112)){
		echo "Binded\n";
		$server->Start();
	}
}

?>